{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "95acce4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch # 导入 torch\n",
    "import torch.nn.functional as F # 导入 nn.functional\n",
    "# 1. 创建两个张量 x1 和 x2\n",
    "x1 = torch.randn(2, 3, 4) # 形状 (batch_size, seq_len1, feature_dim)\n",
    "x2 = torch.randn(2, 5, 4) # 形状 (batch_size, seq_len2, feature_dim)\n",
    "# 2. 计算原始权重\n",
    "raw_weights = torch.bmm(x1, x2.transpose(1, 2)) # 形状 (batch_size, seq_len1, seq_len2)\n",
    "# 3. 用 softmax 函数对原始权重进行归一化\n",
    "attn_weights = F.softmax(raw_weights, dim=2) # 形状 (batch_size, seq_len1, seq_len2)\n",
    "# 4. 将注意力权重与 x2 相乘，计算加权和\n",
    "attn_output = torch.bmm(attn_weights, x2)  # 形状 (batch_size, seq_len1, feature_dim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8fa6cc98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x1: tensor([[[ 0.0971, -0.5204,  0.3034, -0.8555],\n",
      "         [ 1.1608, -1.5700, -0.6016,  0.3667],\n",
      "         [-1.5075,  0.0321, -1.5487, -0.5641]],\n",
      "\n",
      "        [[-1.2064,  0.8690, -0.4074,  0.2248],\n",
      "         [ 1.1715,  0.1240, -0.7232,  0.5079],\n",
      "         [-0.4021,  1.6824, -0.2344,  0.5673]]])\n",
      "x2: tensor([[[ 0.5456,  1.2140,  0.9290, -1.3675],\n",
      "         [ 0.9062, -1.7574, -0.1825,  0.4145],\n",
      "         [-1.3821, -0.9140,  0.6957,  0.0766],\n",
      "         [-0.3361,  0.7526,  1.0915, -0.3276],\n",
      "         [ 0.4812, -0.1446, -0.6791,  0.5942]],\n",
      "\n",
      "        [[-1.5356, -0.2674,  1.5787,  0.3518],\n",
      "         [ 0.7072, -0.2149,  0.6725, -0.9577],\n",
      "         [-0.4562, -0.9658, -0.2043,  0.4609],\n",
      "         [ 1.3268, -0.7196, -0.6243,  1.6647],\n",
      "         [ 0.0198,  1.0432, -0.1927, -2.1842]]])\n"
     ]
    }
   ],
   "source": [
    "# 创建两个张量 x1 和 x2\n",
    "x1 = torch.randn(2, 3, 4) # 形状 (batch_size, seq_len1, feature_dim)\n",
    "x2 = torch.randn(2, 5, 4) # 形状 (batch_size, seq_len2, feature_dim)\n",
    "print(\"x1:\", x1)\n",
    "print(\"x2:\", x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b79f661d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原始权重： tensor([[[ 8.7287e-01,  5.9256e-01,  4.8711e-01,  1.8720e-01, -5.9238e-01],\n",
      "         [-2.3330e+00,  4.0729e+00, -5.5992e-01, -2.3485e+00,  1.4121e+00],\n",
      "         [-1.4509e+00, -1.3736e+00,  9.3359e-01, -9.7488e-01, -1.3552e-02]],\n",
      "\n",
      "        [[ 1.0561e+00, -1.5290e+00, -1.0207e-01, -1.5974e+00,  4.7008e-01],\n",
      "         [-2.7952e+00, -1.7101e-01, -2.7233e-01,  2.7622e+00, -8.1742e-01],\n",
      "         [-2.9749e-03, -1.3468e+00, -1.1321e+00, -6.5329e-01,  5.5310e-01]]])\n"
     ]
    }
   ],
   "source": [
    "# 计算点积，得到原始权重，形状为 (batch_size, seq_len1, seq_len2)\n",
    "raw_weights = torch.bmm(x1, x2.transpose(1, 2))\n",
    "print(\" 原始权重：\", raw_weights) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7fe47f56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 归一化后的注意力权重： tensor([[[0.3154, 0.2383, 0.2145, 0.1589, 0.0729],\n",
      "         [0.0015, 0.9234, 0.0090, 0.0015, 0.0645],\n",
      "         [0.0533, 0.0576, 0.5788, 0.0858, 0.2245]],\n",
      "\n",
      "        [[0.4959, 0.0374, 0.1558, 0.0349, 0.2760],\n",
      "         [0.0034, 0.0470, 0.0424, 0.8826, 0.0246],\n",
      "         [0.2597, 0.0678, 0.0840, 0.1356, 0.4530]]])\n"
     ]
    }
   ],
   "source": [
    "import torch.nn.functional as F # 导入 torch.nn.functional\n",
    "# 应用 softmax 函数，使权重的值在 0 和 1 之间，且每一行的和为 1\n",
    "attn_weights = F.softmax(raw_weights, dim=-1) # 归一化\n",
    "print(\" 归一化后的注意力权重：\", attn_weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "765f34aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 注意力输出 : tensor([[[ 0.0733, -0.1229,  0.5227, -0.3249],\n",
      "         [ 0.8558, -1.6374, -0.2031,  0.4193],\n",
      "         [-0.6394, -0.5333,  0.3829,  0.1005]],\n",
      "\n",
      "        [[-0.7544, -0.0283,  0.7013, -0.3343],\n",
      "         [ 1.1801, -0.6614, -0.5275,  1.3913],\n",
      "         [-0.2004,  0.2098,  0.2665, -0.6985]]])\n"
     ]
    }
   ],
   "source": [
    "# 与 x2 相乘，得到注意力分布的加权和，形状为 (batch_size, seq_len1, feature_dim)\n",
    "attn_output = torch.bmm(attn_weights, x2)\n",
    "print(\" 注意力输出 :\", attn_output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b84776b2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6999321",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2bc56ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
